
import { getMember,downLoadFile, remove, getArea, AllEstateList, querylist } from './allBrokerListServ'

import { parse } from 'qs'
import { message } from 'antd'
import moment from 'moment'
import $ from 'jquery'

import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { hashHistory } from 'dva/router'
import { download } from 'utils/util'
const defultState = {
  loading: false,
  btnLoading: false,
  biz: {
    queryForm: {
      registsTimeBegin:moment(new Date().getTime() - 3600 * 1000 * 24 * 30).format('YYYY-MM-DD'),
      registsTimeEnd:moment(new Date()).format('YYYY-MM-DD')
    },
    ustdvisibe: false,
    why:null,
    area: [],
    projectList: []
  },
}

const tmpModule = {
  state: cloneDeep(defultState),

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {

        if ('' + tmpModule.pathname === '' + pathname) {
          //   dispatch({  type: 'queryInfo', payload: {} }) // end dispatch
             dispatch({ type: 'getArea', payload: {} })
             dispatch({ type: 'AllEstateList', payload: {} })
        }
      })
    },
  },

  effects: {
    * getArea({ payload }, { put, call, select }) {
      let {params,areas}=yield select(state => state[tmpModule.namespace])
      let {success,data} = yield call(getArea)
      if (success) {
          yield put({ type: 'updateStore', payload: { area:data,areas } })
      }
    },
    * AllEstateList({ payload }, { put, call, select }) {
      let tmpState = yield select(state => state[tmpModule.namespace])
      let {params,projectList}=yield select(state => state[tmpModule.namespace])
      let quer = {
        pageNo:1,
        pageSize:1000,
        regionId: tmpState.biz.queryForm.recommenderRegion ?  tmpState.biz.queryForm.recommenderRegion : null
      }
      let {rows,success} = yield call(AllEstateList, quer)
      if (success) {
        console.log(rows,321)
        projectList = rows.length > 0 ? rows.map(i => {
          return {
            label: i.projectName,
            value: i.productId
          }
        }):[]
        yield put({ type: 'updateStore', payload: { projectList } })
      }
    },
    // *querylist ({ payload }, { put, call, select }) {
    //   let tmpState = yield select(state => state[tmpModule.namespace])
    //   let {dataSource} = tmpState.biz.tableData
    //   let quer = {
    //     ...tmpState.biz.queryForm,
    //     pageNo:1,
    //     pageSize:10,
    //   }
    //   console.log(dataSource,1)
    //   let {rows,success} = yield call(querylist, quer)
    //   if (success) {
    //     dataSource = rows
    //     console.log(dataSource,22222222222222222)
    //     yield put({ type: 'updateStore', payload: { dataSource } })
    //   }
    // },
    *setFormVal({ payload }, { put, call, select }) {
      let { biz } = yield select(d => d[tmpModule.namespace])

      try {
        yield put({ type: 'updateStore', payload: { biz: payload } })
      } catch (e) {
        console.error(e)
      }
    },
    // 查询记录
    *queryInfo({ payload }, { put, call, select }) {
      try {
        // 获取当前组件状态机中的数据，也可以获取其他组件的状态机数据，只要他们被创建过
        let tmpState = yield select(state => state[tmpModule.namespace])
        //调用接口，并传参
        const { data, resultCode, resultMsg } = yield call(getMember, parse({ xxx: "1" }))
        // 调完接口，将返回的数据存入到状态机
        yield put({ // 调用reducers中方法，把新数据与状态机数据合并更新
          type: 'updateStore', // 方法名，也可以调用effects中的方法
          payload: {
            xxx: data
          }
        })
      } catch (e) {
        console.error(e)
      }
    },
    *remove ({payload}, {put, call, select}) {
      try{
        let tmpState = yield select(state => state[tmpModule.namespace])
        let {selectedRows} = tmpState.biz
        let quer = {
          brokerIdList: selectedRows.map(i => {return i.brokerId}),
          expireRemark: tmpState.why
        }
        let { data, success } = yield call(remove, quer);
        if(success){
          message.success('解除绑定成功');
        }
      } catch (e) {
        console.error(e)
      }
    },
    //导出
    * downLoad ({payload}, {put, call, select}) {
      try {
        let tmpState = yield select(state => state[tmpModule.namespace])
        let {queryForm} = tmpState.biz
        const {extParams, success} = yield call(downLoadFile, queryForm)
        if (success) {
          // yield put({ type: 'updateStore', payload: {  data } });
          // window.open(data);
         
          let url = extParams.href || ''
          if (url) {
            var $a = $("<a>");
            $a.attr("href", url);
            $a.attr("download", '经纪人注册');
            $("body").append($a);
            $a[0].click();
            $a.remove();
            //console.log(data)
            message.success('操作成功')
          }
        }
      } catch (e) {
        console.error(e)
      }
    },

  },

  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
